import * as THREE from 'three';
import { ArcballControls } from '@/modelTransformation/ArcballControls';

const createLight = () => {
  // // 环境光
  var ambientLight = new THREE.AmbientLight(0xffffff, 0.2);

  // 点光源
  const point = new THREE.PointLight(0xffffff);
  point.position.set(0, 0, 200); // 点光源位置

  return {
    point,
    ambientLight,
  };
};

const createCamera = (element) => {

   const width = element.clientWidth;
  const height = element.clientHeight;

  // 计算正交相机的左右上下边界
  const distance = 15;
  const left = -width / distance;
  const right = width / distance;
  const top = height / distance
  const bottom = -height / distance;

  const near = 0.1;
  const far = 1000;

  const camera = new THREE.OrthographicCamera(left, right, top, bottom, near, far);

  // 设置初始位置
  camera.position.set(0, 0, 200); // Z轴方向远离模型
  camera.lookAt(0, 0, 0); // 看向原点

  return { camera };
};

// 创建渲染器
const createRender = (self, element) => {
  self.renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: false,
    preserveDrawingBuffer: true,
  });
  self.renderer.outputEncoding = THREE.sRGBEncoding;
  self.renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
  self.renderer.setPixelRatio(window.devicePixelRatio * 1); // 更新渲染器的像素比
  self.renderer.setClearColor(new THREE.Color('0xffffff'), 1.0); // 设置背景颜色
  element.appendChild(self.renderer.domElement);
};
// 创建控件对象
const createControls = (camera, renderer, openaxes = false) => {
  const controls = new ArcballControls(camera, renderer.domElement);
  let axesHelper = null;
  if (openaxes) {
    axesHelper = new THREE.AxesHelper(50);
    const colorsX = new THREE.Color(1, 0, 0);
    const colorsY = new THREE.Color(0, 1, 0);
    const colorsZ = new THREE.Color(0, 0, 1);
    axesHelper.setColors(colorsX, colorsY, colorsZ);
  }

  return {
    controls,
    axesHelper,
  };
};
export { createLight, createCamera, createRender, createControls };
